//
//  SDHSportsInteListVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/5.
//

#import "SDHSportsInteListVC.h"
#import "SDHSportsInteCell.h"
#import "SDHSportsInteDetailVC.h"
#import "SDHSportsInteModel.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "MJRefresh.h"
#import "SDHSportsInteListModel.h"
#import "MJExtension.h"

@interface SDHSportsInteListVC () <UITableViewDelegate, UITableViewDataSource> {
    SDHSportsInteModel *model;
    SDHSportsNetRequest *netRequest;
    BOOL isRefresh;
    NSInteger pageNum;
}

@property (nonatomic, strong) UITableView *intelTable;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation SDHSportsInteListVC

- (id)init {
    if (self = [super init]) {
        isRefresh = YES;
        pageNum = 0;
        _dataArray = [NSMutableArray array];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(56, 56, 56);
    
    UITableView *table = [UITableView new];
    table.dataSource = self;
    table.delegate = self;
//    table.backgroundColor = [UIColor redColor];
    [self.view addSubview:table];
    self.intelTable = table;
    UIView *view = [UIView new];
    self.tableView.tableFooterView = view;
    
    __weak typeof(self) weakSelf = self;
    [self.intelTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).insets(UIEdgeInsetsZero).priorityLow();
        make.bottom.equalTo(weakSelf.view).offset(-50);
    }];
    
    model = self.tabInfoDic[@"info"];
    if (model == nil) {
        model = [[SDHSportsInteModel alloc] init];
        model.league_name = @"世俱杯";
    }
    
    self.intelTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf headerRefreshing];
    }];
    self.intelTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefreshing];
    }];

    [self loadData];
}

- (void)headerRefreshing {
    isRefresh = YES;
    pageNum = 0;
    [self loadData];
}

- (void)footerRefreshing {
    pageNum++;
    [self loadData];
}

- (void)endRefreshing {
    isRefresh = NO;
    [self.intelTable reloadData];

    [MBProgressHUD hideHUD];
    [self.intelTable.mj_header endRefreshing];
    [self.intelTable.mj_footer endRefreshing];
    
    if (self.intelTable.contentSize.height < self.intelTable.size.height) {
        self.intelTable.mj_footer.hidden = YES;
        UIView *tmpV = [UIView new];
        self.intelTable.tableFooterView = tmpV;
    }
}

- (void)loadData {
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];
    request.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyusaishi/getIntelligenceList";
    request.requestPara = @{@"league_name":model.league_name, @"page":@(pageNum)};
    
    [MBProgressHUD showLoading];
    
    __weak typeof(self) weakSelf = self;
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        NSArray *array = [SDHSportsInteListModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
        if (isRefresh) {
            [weakSelf.dataArray removeAllObjects];
        }
        [weakSelf.dataArray addObjectsFromArray:array];
        
        [weakSelf endRefreshing];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endRefreshing];
    } failureBlock:^{
        [weakSelf endRefreshing];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    self.tableView.frame = self.view.bounds;
}

#pragma - uitableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 140;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsInteCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsInteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    SDHSportsInteListModel *model = [_dataArray objectAtIndex:indexPath.row];
    [cell updateContent:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsInteDetailVC *vc = [[SDHSportsInteDetailVC alloc] init];
    SDHSportsInteListModel *model = [_dataArray objectAtIndex:indexPath.row];
    vc.model = model;
    vc.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
